/**
 * @author liudm
 */

function ajaxTodoDynamic(obj,rel){
    var $this=$(this);
    var url=unescape($this.attr("href")).replaceTmById($(event.target).parents(".unitBox:first"));
    DWZ.debug(url);
    if(!url.isFinishedTm()){
    alertMsg.error($this.attr("warn")||DWZ.msg("alertSelectMsg"));
    return false;}
    //var rel=$this.attr("rel");
    var title=$this.attr("title");
   /* if(title){
    alertMsg.confirm(title,{
    okCall:function(){
    ajaxTodo(url,$this.attr("callback"));}});}else{
    ajaxTodo(url,$this.attr("callback"));}*/
    alert(rel);
    alert($("#"+rel).html());
    $("#"+rel).html("<!--#include virtual='"+$this.attr("href")+"' -->");
    alert($("#"+rel).html());
    //event.preventDefault();
}
function getmyjTree(data, pId) {
    var tree;
    if (pId == -1) {
        tree = '<ul id="browser" class="filetree"><li><span class="folder">客户/设备列表</span>';
    } else {
        tree = '<ul class="filetree">';
    }
    if (pId != -1) {
        for (var i in data) 
        {
        	var item = data[i];
        	if (item.pId == pId) {
                if (item.type == 1) {                                                                    //onclick='ajaxTodoDynamic(this,'"+item.rel+"')'
                    tree += '<li><a href="' + item.url + '" target="ajax" rel="' + item.rel + '" ><span class="file">' + item.name + '</span></a>';
                } else {
                    tree += '<li><a><span class="folder">' + item.name + '</span></a>';
                    if (item.haschild)
                        tree += getTree(data, item.id);
                }
                tree += "</li>";
            }
        }
    } else {
        tree += getmyjTree(data, 0);
    }
    if (pId == -1)
        return tree + "</li></ul>";
    else
        return tree + "</ul>";
}
function hasDeptRight(deptid)
{
	if($.cookie('currentusercode')=='admin')
		return true;
	var strDeptId = $.cookie('rightDeptIdListJson');
	var deptJson = JSON.parse(strDeptId);
	for (var i in deptJson.rightDeptIdList)
	{
		//alert(deptJson.rightDeptIdList[i]);
		if(deptJson.rightDeptIdList[i]==deptid)
			return true;
	}
	return false;
}
function getTree(data,pId){ 
	var tree;
    if(pId==-1){   
        tree = '<ul class="tree treeFolder"><li><a>客户/设备列表</a>';   
    }else{   
        tree = '<ul>';   
    }   
    if(pId!=-1)
    {
    	 for (var i in data) 
         {
    		 var item = data[i];
    		 if(item.pId==0)
    		 {
    	    	if(!hasDeptRight(item.id))
    	    		continue;
    		 }
        	if(item.pId == pId ){   
            	if(item.haschild)
                {
                	tree += '<li><a>'+item.name+'</a>';
                	tree += getTree(data,item.id);
                }
                else
                 {
                    if(item.url!=""&&item.type==1) 
                    	tree += "<li><a href='"+item.url+"' target='ajax' rel='"+item.rel+"' >"+item.name+"<i class='fa fa-bus fa-lg fa-fw'></i></a>";
                    else
                        tree += '<li><a>'+item.name+'</a>';    
                 }   
                tree += "</li>";   
            }   
        } 
    } 
    else
    {
    	tree += getTree(data,0);
    } 
    if (pId == -1)
        return tree + "</li></ul>";
    else
        return tree + "</ul>";
}


function ajaxbuildTree(surl,ctrl) {
	$.ajax({
        type: "post",
        contentType: "application/json",
        dataType: "json",
        timeout: 1000, //超时时间设置，单位毫秒
        url: surl,
        data: {},
        success: function(data) {
            if (data.success) {
            	buildTree(data.deptNodeList,ctrl);
            }
        },
        complete: function(status) { //请求完成后最终执行参数
            　　　　
            if (status == 'timeout') { //超时,status还有success,error等值的情况
            	console.info("ajaxbuildMainMenu timeout");　　　
            }　　
        },
        error: function( textStatus, errorThrown) {
        	console.info("ajaxbuildMainMenu "+textStatus);　　　　　　　　
        }
    });
}

function buildTree(data,ctrl)
{
	var treecontent = getTree(data, -1).replaceAll("<ul></ul>", "");
    $(ctrl).html(treecontent);
}

function ajaxbuildMainMenu(surl,ctrl,rightdata) {
	var content = "";
	$.ajax({
        type: "post",
        contentType: "application/json",
        dataType: "json",
        async:false, 
        timeout: 1500, //超时时间设置，单位毫秒
        url: surl,
        data: {},
        success: function(data) {
            if (data.success) {
            	content = getMainMenuHtml(data.sysFunctionList,rightdata);
            	$(ctrl).html(content);
            }
        },
        complete: function(status) { //请求完成后最终执行参数
            　　　　
            if (status == 'timeout') { //超时,status还有success,error等值的情况
            	console.info("ajaxbuildMainMenu timeout");　　　　
            }　　
        },
        error: function( textStatus, errorThrown) {
        	console.info("ajaxbuildMainMenu "+textStatus);　　　　　　　
        }
    });
}

/*
<div class="accordionHeader"><h2><span>Folder</span>系统设置
</h2></div>
<div class="accordionContent"><ul class="tree treeFolder ">
		<li><a href="willok.jsp" target="navTab" rel="paraminfo">参数管理</a></li>
		<li><a href="willok.jsp" target="navTab" rel="databackup">数据备份</a></li>
		<li><a href="willok.jsp" target="navTab" rel="datarestore">数据恢复</a></li>
</ul></div>*/ 
//构造主菜单html内容
function getMainMenuHtml(data,rightdata)
{
	var content = '';
	for (var i in data) 
    {
    	var item = data[i];
    	if(item.upperid==0)
    	{
    		content += '<div class="accordionHeader" id="menu'+item.funcid+'"><h2><span>Folder</span>' + item.funcname + '</h2></div>' ;
    		content += getFuncHtml(data,item.funcid,rightdata);	
    	}
    }
	return content;
}

// 返回：1 读写权限  0:只读权限  -1:无权限
function getRightFlag(rightdata,funcid )
{
	var srcfuncid = funcid;
	for (var i in rightdata.sysUserFuncList) 
	{
		if(srcfuncid==rightdata.sysUserFuncList[i].funcid)
	    {
	    	if(rightdata.sysUserFuncList[i].dataright==0)
	    		return 0;
	    	else
	    		return 1;
	    }
	}
	return -1;
}
function getFuncHtml(data,upperid,rightdata)
{
	var content = '<div class="accordionContent"><ul class="tree treeFolder ">';
	var itemcontent='';
	for (var i in data) 
    {
		var item = data[i];
    	if(item.upperid==upperid)
    	{	
    		var licontent = '';
    		var right = getRightFlag(rightdata,item.funcid);
    		//alert('right:'+right);
    		if(right==1||right==0||$.cookie('currentusercode')=='admin')
    			licontent = '<li><a id="menuitem'+item.funcid+'" href="'+item.funcref+'" target="navTab" rel="'+item.functarget+'">'+item.funcname+'</a></li>';
    		else
    			licontent = '<li><a id="menuitem'+item.funcid+'" href="javascript:;" onclick=alertMsg.warn("无此功能操作权限!") >'+item.funcname+'(无权限)</a></li>';
    			
    		itemcontent += licontent;
    	}
    }
	return content+itemcontent+'</ul></div>';
}

function downLoadJs(url){  
    var elem = document.createElement("script");  
    elem.src = url;  
    document.body.appendChild(elem);  
}  

function downLoadCss(url){  
    var elem = document.createElement("link");  
    elem.rel = "stylesheet";  
    elem.type = "text/css";  
    elem.href = url;  
    document.body.appendChild(elem);  
}  
//动态构造角色功能表格
function ajaxbuildFuncTable(id,ctrl,type) {
	var content = "";
	var surl = "jsonQuerySysRoleFunc?roleid="+id;
	if(type=='user')
		surl = "jsonQuerySysUserFunc?userid="+id;
	$.ajax({
        type: "post",
        contentType: "application/json",
        dataType: "json",
        async:false, 
        timeout: 1500, //超时时间设置，单位毫秒
        url: surl,
        data: {},
        success: function(data) {
            if (data.success) {
            	content = getRoleFuncTable(data.sysFunctionList);
            	$(ctrl).html(content);
            }
        },
        complete: function(status) { //请求完成后最终执行参数
            if (status == 'timeout') { //超时,status还有success,error等值的情况
            	console.info("ajaxbuildMainMenu timeout");　　　　
            }　　
        },
        error: function( textStatus, errorThrown) {
        	console.info("ajaxbuildMainMenu "+textStatus);　　　　　　　
        }
    });
}


function getRoleFuncTable(data)
{
	var tablecontent='';
	var num = 1;
	var sel = 'checked="true"';
	var levle = "";
	var td1='',td2='',td3='',td4='';
	for (var i in data) 
    {
		var item = data[i];
		var attrsel="",attrright="";
		if(item.level==1)
			attrsel = sel;
		if(item.righttype==1)
			attrright = sel;
		td1 = '<td><input type="checkbox" name="chk'+item.funcid+'" value="'+item.funcid+'" '+attrsel+' />'+item.funcid+'</td>';
		td2 = '<td>'+item.funccode+'</td>';
		if(item.upperid!=0)
		{
			levle = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;';
			td3 = '<td>'+levle+'<input type="checkbox" id="chk'+item.funcid+'" value="'+item.funcid+'" '+attrsel+' />'+item.funcname+'</td>';
			td4 = '<td><input type="checkbox" id="chkright'+item.funcid+'" value="'+item.dataright+'" '+attrright+' />数据维护</td>';
		}
		else
		{
			td3 = '<td>'+item.funcname+'</td>';
			td4 = '<td></td>';
		}
		var tr = '<tr target="sid_rolefunc" rel="'+num+'">'+td3+td4+'</tr>';
		tablecontent += tr;
    }
	return tablecontent;
}

//动态构造角色功能表格
function ajaxbuildRightRole(userid,usercode,ctrl) {
	var content = "";
	var surl = "jsonQueryRightSysRole?userid="+userid+"&usercode="+usercode;
	$.ajax({
      type: "post",
      contentType: "application/json",
      dataType: "json",
      async:false, 
      timeout: 1500, //超时时间设置，单位毫秒
      url: surl,
      data: {},
      success: function(data) {
          if (data.success) {
          	content = getRightRoleTable(data.sysRoleList);
          	$(ctrl).html(content);
          }
      },
      complete: function(status) { //请求完成后最终执行参数
          if (status == 'timeout') { //超时,status还有success,error等值的情况
          	console.info("ajaxbuildRightRole timeout");　　　　
          }　　
      },
      error: function( textStatus, errorThrown) {
      	console.info("ajaxbuildRightRole "+textStatus);　　　　　　　
      }
  });
}

function getRightRoleTable(data)
{
	var tablecontent='',attrsel='';
	var sel = 'checked="true"';
	var td1='',td2='';
	for (var i in data) 
    {
		var item = data[i];
		if(item.isvalid==1)
			attrsel = sel;
		else
			attrsel = '';
		td1 = '<td><input type="checkbox" id="chk'+item.roleid+'" value="'+item.roleid+'" '+attrsel+' />'+item.rolecode+'</td>';
		td2 = '<td>'+item.rolename+'</td>';
		var tr = '<tr target="sid_role" rel="'+item.roleid+'">'+td1+td2+'</tr>';
		tablecontent += tr;
    }
	return tablecontent;
}

function canWriteData(funccode)
{
	var strRight = $.cookie('sysUserFuncListJson');
	if($.cookie('currentusercode')=='admin')
		return 1;
	if(strRight.indexOf(funccode+',1') != -1) 
		return 1;
	else if(strRight.indexOf(funccode+',0') != -1) 
		return 0;
	else
		return -1;
}


